package com.future.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.allUtils.constants.PermissionConstants;
import com.future.domain.Announcement;

import com.future.domain.Article;
import com.future.domain.DTO.AnnouncementDTO;
import com.future.domain.Result;
import com.future.service.AnnouncementService;

import com.future.allUtils.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(tags = "公告管理")
@RestController
@RequestMapping("/Announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 发布公告
     * @param announcementDTO 公告DTO
     * @return 操作结果
     */
    @ApiOperation("发布公告")
    @PostMapping("/publish")
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_ANNOUNCEMENT_PUBLISH + "') or hasRole('ADMIN')")
    public Result publishAnnouncement(@RequestBody AnnouncementDTO announcementDTO) {
        return announcementService.publishAnnouncement(announcementDTO);
    }
    /**
     * 查询所有公告
     * @return 公告列表
     */
    @ApiOperation("查询所有公告")
    @PostMapping("/get")
    public Result getAnnouncements(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<Announcement> page = new Page<>(pageNum, pageSize);
        return announcementService.getAnnouncements(page);
    }


    /**
     * 根据ID查询公告
     * @param id 公告ID
     * @return 公告
     */
    @ApiOperation("根据ID查询公告")
    @PostMapping("/getById")
    public Result getAnnouncementById(Long id) {
        return announcementService.getAnnouncementById(id);
    }
    /**
     * 根据名称查询公告
     * @param name 公告名称
     * @return 公告
     */
    @ApiOperation("根据名称查询公告")
    @PostMapping("/getByName")
    public Result getAnnouncementsByName(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam String name) {
        IPage<Announcement> page = new Page<>(pageNum, pageSize);
        return announcementService.getAnnouncementsByName(page, name);
    }

    @ApiOperation("公告置顶")
    @PostMapping("/top")
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_ANNOUNCEMENT_PUBLISH + "') or hasRole('ADMIN')")
    public Result top(Long id) {
        return announcementService.top(id);
    }

    @ApiOperation("公告取消置顶")
    @PostMapping("/cancelTop")
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_ANNOUNCEMENT_PUBLISH + "') or hasRole('ADMIN')")
    public Result cancelTop(Long id) {
        return announcementService.cancelTop(id);
    }
    /**
     * 获取最新公告
     * @return 最新公告
     */
    @ApiOperation("获取最新公告")
    @PostMapping("/getLatest")
    public Result getLatestAnnouncement() {
        return announcementService.getLatestAnnouncement();
    }
    /**
     * 删除公告
     * @param id 公告ID
     * @return 操作结果
     */
    @ApiOperation("删除公告")
    @PostMapping("/delete")
    @PreAuthorize("hasAnyAuthority('" + PermissionConstants.ADMIN_ANNOUNCEMENT_PUBLISH + "') or hasRole('ADMIN')")
    public Result delete(Long id) {
        return announcementService.removeById(id) ? Result.ok() : Result.fail("删除失败");
    }
    /**
     * 获取置顶公告
     * @return 置顶公告
     */
    @ApiOperation("获取置顶公告")
    @PostMapping("/getPinned")
    public Result getPinnedAnnouncements() {
        return announcementService.getPinnedAnnouncements();
    }
}
